package dao

import (
	"fmt"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

type t_device struct {
}

var T_device = t_device{}

func (a *t_device) GetList(where ...string) (list gdb.List, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_device")
	if len(where) == 0 {
	} else {
		model = model.Where(where[0])
	}
	res, err := model.All()
	return res.List(), err
}

func (a *t_device) GetOne(where string) (one gdb.Map, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_device")
	res, err := model.Where(where).One()
	if err != nil {
		return nil, err
	} else {
		if res == nil {
			err = fmt.Errorf("未找到该设备")
		} else {
			one = res.Map()
		}
	}
	return one, err
}

func (a *t_device) GetAddrArr(where ...string) (arr []string) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("v_device").Fields("device_addr")
	if len(where) == 0 {
	} else {
		model = model.Where(where[0])
	}
	res, _ := model.All()
	_list := res.List()
	for _, v := range _list {
		arr = append(arr, gconv.String(v["device_addr"]))
	}
	return arr
}

func (a *t_device) GetHousenameArr(where ...string) (nameArr []string, addrArr []string) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("v_device").Fields("housename,device_addr")
	if len(where) == 0 {
	} else {
		model = model.Where(where[0])
	}
	res, _ := model.All()
	_list := res.List()
	for _, v := range _list {
		nameArr = append(nameArr, gconv.String(v["housename"]))
		addrArr = append(addrArr, gconv.String(v["device_addr"]))
	}
	return nameArr, addrArr
}

func (a *t_device) GetFarmnameArr(where ...string) (nameArr []string, addrArr []string) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("v_device").Fields("farmname,device_addr")
	if len(where) == 0 {
	} else {
		model = model.Where(where[0])
	}
	res, _ := model.All()
	_list := res.List()
	for _, v := range _list {
		nameArr = append(nameArr, gconv.String(v["farmname"]))
		addrArr = append(addrArr, gconv.String(v["device_addr"]))
	}
	return nameArr, addrArr
}

func (a *t_device) GetTopoNameArr(where ...string) (farmNameArr []string, houseNameArr []string, addrArr []string) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("v_device").Fields("farmname,housename,device_addr")
	if len(where) == 0 {
	} else {
		model = model.Where(where[0])
	}
	res, _ := model.All()
	_list := res.List()
	for _, v := range _list {
		farmNameArr = append(farmNameArr, gconv.String(v["farmname"]))
		houseNameArr = append(houseNameArr, gconv.String(v["housename"]))
		addrArr = append(addrArr, gconv.String(v["device_addr"]))
	}
	return farmNameArr, houseNameArr, addrArr
}

func (a *t_device) Save(data interface{}) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, err = db.Model("t_device").Data(data).Save()
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func (a *t_device) Update(device_type string, device_addr string, alive int, device_ip string, lasttime string, version string, alarm string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	one, err := db.GetOne(fmt.Sprintf(" select * from t_device where device_type='%s' and device_addr='%s' ", device_type, device_addr))
	if one == nil {
		/*item := gdb.Map{
			"farmcode":    nil,
			"device_type": device_type,
			"device_addr": device_addr,
			"alive":       alive,
			"device_ip":   device_ip,
			"lasttime":    lasttime,
			"version":     version,
			"alarm":       alarm,
		}
		res, errors := db.Model("t_device").Data(item).Insert()
		if errors != nil {
			err = errors
		} else if res != nil {
		}*/
	} else {
		item := one.Map()
		item["alive"] = alive
		item["device_ip"] = device_ip
		item["lasttime"] = lasttime
		item["version"] = version
		_, err = db.Model("t_device").Where("id=" + gconv.String(item["id"])).Data(item).Update()
	}
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func (a *t_device) Delete(where ...string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_device")
	if len(where) == 0 {
		_, err = model.Delete()
	} else {
		_, err = model.Delete(where)
	}
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

// GetDeviceParentID 根据设备addr查询houseid+farmid
func (a *t_device) GetDeviceParentID(device_type string, device_addr string) (houseid interface{}, farmid interface{}, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	//只对已经绑定的
	model := db.Model("t_device")
	res, error := model.Where(fmt.Sprintf("device_type='%s' and device_addr='%s'", device_type, device_addr)).One()
	if error != nil {
		err = error
	} else {
		if res != nil {
			item := res.Map()
			houseid = item["houseid"]
			if houseid != nil {
				model = db.Model("t_house")
				res, error = model.Where(fmt.Sprintf("id='%s'", gconv.String(houseid))).One()
				if error != nil {
					err = error
				} else {
					if res != nil {
						item1 := res.Map()
						farmid = item1["farmid"]
					}
				}
			}
		}

	}
	return houseid, farmid, err
}
